class InstallationsController < ApplicationController
  
  def index
    @installations  = Installation.order('installed_on desc')
  end
  
  def show
    @install = Installation.find(params[:id])
    @order = @install.order
    @buyer = @order.buyer
  end
  
  def new 
    @install = Installation.new(:order_id => params[:order_id])
  end
  
  def create
    @install = Installation.create(:order_id => params[:order_id])
    redirect_to edit_installation_url(@install)
  end
  
  def edit
    @install = Installation.find(params[:id])
    @order = @install.order
    @buyer = @order.buyer
    respond_to do |format|
      format.html do
      end
      format.js do
      end
    end
  end

  def update
    @install = Installation.find(params[:id])
    render :edit if not @install.update_attributes(params[:installation])
  end

  def new_install_items
    @install = Installation.find(params[:id])
  end

  def create_install_items
    @install = Installation.find(params[:id])
    @stat_order_items = FurnitureLogisticStatOrderItem.find(params[:stat_order_item_id])
    @stat_order_items.each_with_index.each do |stat_order_item,index|
      InstallItem.create( :installation_id => @install.id,
        :order_id => stat_order_item.order_item.order_id,
        :order_item_id => stat_order_item.order_item_id,
        :furniture_id => stat_order_item.furniture_logistic_stat.furniture_id,
        :furniture_logistic_stat_order_item_id => stat_order_item.id,
        :has_been_sent_to_installer_count => params[:sent_count][index]
      )   
    end
    @install.reload
  end
  
  def edit_install_item
    @install_item = InstallItem.find(params[:id])
  end
  
  def update_install_item
    @install_item = InstallItem.find(params[:id])
    @install = @install_item.installation
    render :edit_install_item if not @install_item.update_attributes(params[:install_item])
  end
  
  def destroy_install_item
    @install_item = InstallItem.find(params[:id])
    @install_item.destroy
  end

  def install_items
    @install = Installation.find(params[:id])
  end
  
  def create_installed
    @install_item = InstallItem.find(params[:id])
    @install_item.installed!
    @install = @install_item.installation
  end
  
  def destroy_installed
    @install_item = InstallItem.find(params[:id])
    @install_item.installed_to_has_been_sent_to_installer!
    @install = @install_item.installation
  end
  
  def edit_after_serviced
    @install_item = InstallItem.find(params[:id])
    if @install_item.after_serviced_count == 0
      @install_item.after_serviced_count = 1
      @install_item.installed_count = @install_item.has_been_sent_to_installer_count - @install_item.after_serviced_count
    end
  end
  
  def update_after_serviced
    @install_item = InstallItem.find(params[:id])
    @install = @install_item.installation
    render :edit_after_serviced if not @install_item.after_serviced!( params[:install_item])
  end
  
  def destroy_after_serviced
    @install_item = InstallItem.find(params[:id])
    @install_item.after_serviced_to_has_been_sent_to_installer!
    @install = @install_item.installation
  end
end
